package com.project.controller;

import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.project.bean.SupplierBean;
import com.project.service.ISupplierService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("supplier")
public class SupplierController {
    @Autowired
    private ISupplierService supplierService;

    @RequestMapping("findByItem")
    public IPage<SupplierBean> findByItem(Integer pageNo, String name, String business) {
        return supplierService.findByItem(pageNo, name, business);
    }

    @RequestMapping("findById")
    public SupplierBean findById(Integer id) {
        return supplierService.findById(id);
    }

    @RequestMapping("update")
    public String update(Integer id, String user, String phone) {
        supplierService.update(id, user, phone);
        return "ok";
    }

    @RequestMapping("add")
    public String add(SupplierBean supplier) {
        supplierService.add(supplier);
        return "ok";
    }

    @RequestMapping("findAll")
    public List<SupplierBean> findAll() {
        return supplierService.findAll();
    }

    @RequestMapping("addList")
    public String addList(List<SupplierBean> supplierList) {
        supplierService.addList(supplierList);
        return "ok";
    }

    @RequestMapping("addFile")
    public String addFile(@RequestParam("excelFile") MultipartFile mf) throws Exception {
        ImportParams ims = new ImportParams();

        ims.setTitleRows(1);

        ims.setHeadRows(1);

        ims.setSheetNum(1);

        List<SupplierBean> list = ExcelImportUtil.importExcel(mf.getInputStream(), SupplierBean.class, ims);
        supplierService.addList(list);
        return "ok";
    }
}
